package com.aviccxzx.smarthome.UserModule;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.GosUserModuleBaseActivity;
import com.aviccxzx.smarthome.entity.AddSceneBean;
import com.aviccxzx.smarthome.entity.GetSceneTime;
import com.aviccxzx.smarthome.entity.SaveSceneBean;
import com.aviccxzx.smarthome.entity.SceneListAskBean;
import com.aviccxzx.smarthome.netutil.app.App;
import com.aviccxzx.smarthome.picker.TimePicker;
import com.aviccxzx.smarthome.service.RetrofitHelper;
import com.aviccxzx.smarthome.service.RetrofitService;
import com.aviccxzx.smarthome.utils.ChangeNameDialogUtil;
import com.aviccxzx.smarthome.utils.DialogUtil;
import com.google.gson.Gson;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by joey on 217/2/22.
 */

public class SetSceneAct extends GosUserModuleBaseActivity implements View.OnClickListener {
    TextView title_right_text, start_time, end_time, sence_name_tv;
    CheckBox text1, text2, text3, text4, text5, text6, text7;
    CheckBox scene_switch_img;
    String scene_name, scene_switch, scene_id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.setscene_timer_lay_v19);
        }
        initView();
        setListener();
        getSceneInfo();
    }

    public void getSceneInfo() {
        progressDialog.show();
        RetrofitService service = RetrofitHelper.getInstance(this).getServer();
        Gson gson = new Gson();

        String uid = spf.getString("Uid", "");
        String Mac = spf.getString("gatemac", "");
        String imei = spf.getString("Imei", "");

        SceneListAskBean scene = new SceneListAskBean();
        scene.setImei(imei);
        scene.setMac(Mac);
        scene.setUid(uid);

        String strEntity = gson.toJson(scene);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        Call<GetSceneTime> call = service.getSceneInfo(body, scene_id);
        call.enqueue(new Callback<GetSceneTime>() {
            @Override
            public void onResponse(Call<GetSceneTime> call, Response<GetSceneTime> response) {
                /**
                 * {
                 "code": 0,
                 "data": {
                 "begin_time": "string",
                 "end_time": "string",
                 "remark": "string",
                 "scene_id": "string",
                 "scene_model": 0,
                 "scene_name": "string",
                 "scene_switch": 0,
                 "weeks": "string"
                 },
                 "message": "string"
                 }
                 */
                if (response.body()!=null&&response.body().getData()!=null){
                    if (!TextUtils.isEmpty(response.body().getData().getScene_name())) {
                        sence_name_tv.setText(response.body().getData().getScene_name());
                    }
                    if (!TextUtils.isEmpty(response.body().getData().getBegin_time())) {
                        start_time.setText(response.body().getData().getBegin_time());
                    }
                    if (!TextUtils.isEmpty(response.body().getData().getEnd_time())) {
                        end_time.setText(response.body().getData().getEnd_time());
                    }
                    if (response.body().getData().getScene_switch() == 0) {
                        scene_switch_img.setChecked(false);
                    } else {
                        scene_switch_img.setChecked(true);
                    }
                    if (!TextUtils.isEmpty(response.body().getData().getWeeks())) {
                        String weeks = response.body().getData().getWeeks();
                        if (weeks.contains("1")) {
                            text1.setChecked(true);
                        }
                        if (weeks.contains("2")) {
                            text2.setChecked(true);
                        }
                        if (weeks.contains("3")) {
                            text3.setChecked(true);
                        }
                        if (weeks.contains("4")) {
                            text4.setChecked(true);
                        }
                        if (weeks.contains("5")) {
                            text5.setChecked(true);
                        }
                        if (weeks.contains("6")) {
                            text6.setChecked(true);
                        }
                        if (weeks.contains("7")) {
                            text7.setChecked(true);
                        }
                    }

                }

                progressDialog.dismiss();
            }

            @Override
            public void onFailure(Call<GetSceneTime> call, Throwable t) {
                progressDialog.dismiss();
            }
        });
    }

    private void setListener() {
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        findViewById(R.id.title_right_text).setOnClickListener(this);
//        findViewById(R.id.clean_timer_btn).setOnClickListener(this);
        start_time.setOnClickListener(this);
        end_time.setOnClickListener(this);
        text1.setOnClickListener(this);
        text2.setOnClickListener(this);
        text3.setOnClickListener(this);
        text4.setOnClickListener(this);
        text5.setOnClickListener(this);
        text6.setOnClickListener(this);
        text7.setOnClickListener(this);
        sence_name_tv.setOnClickListener(this);
        scene_switch_img.setOnClickListener(this);

    }

    private void initView() {
        ((TextView) findViewById(R.id.title_tv)).setText("编辑场景");
        title_right_text = (TextView) findViewById(R.id.title_right_text);
        title_right_text.setVisibility(View.VISIBLE);
        title_right_text.setText("保存");
        start_time = (TextView) findViewById(R.id.start_time);
        end_time = (TextView) findViewById(R.id.end_time);
        text1 = (CheckBox) findViewById(R.id.text1);
        text2 = (CheckBox) findViewById(R.id.text2);
        text3 = (CheckBox) findViewById(R.id.text3);
        text4 = (CheckBox) findViewById(R.id.text4);
        text5 = (CheckBox) findViewById(R.id.text5);
        text6 = (CheckBox) findViewById(R.id.text6);
        text7 = (CheckBox) findViewById(R.id.text7);
        sence_name_tv = (TextView) findViewById(R.id.sence_name_tv);
        scene_switch_img = (CheckBox) findViewById(R.id.scene_switch_img);

//        scene_name = getIntent().getStringExtra("Name");
//        scene_switch = getIntent().getStringExtra("Switch");
        scene_id = getIntent().getStringExtra("sceneId");
    }
    int start_hour=9,start_minute=0,end_hour=18,end_minute=0;
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.title_right_text:
                //判断时间的合理性
                if(end_hour-start_hour>0||(end_hour-start_hour==0&&end_minute-start_minute>0)){

                }else {
                    showToast("结束时间要晚于开始时间");
                    return;
                }
                saveScene("save");
                break;
            case R.id.start_time:
                TimePicker start = new TimePicker(this, TimePicker.HOUR_24);
                start.setGravity(Gravity.CENTER);//弹框居中
                start.setRangeStart(0, 0);//09:00
                start.setRangeEnd(23, 0);//18:30
                start.setTopLineVisible(false);
                start.setOnTimePickListener(new TimePicker.OnTimePickListener() {
                    @Override
                    public void onTimePicked(String hour, String minute) {
                        start_hour = Integer.valueOf(hour);
                        start_minute = Integer.valueOf(minute);
                        start_time.setText(hour + ":" + minute);
                        showToast(hour + ":" + minute);
                    }
                });
                start.show();
                break;
            case R.id.end_time:
                TimePicker end = new TimePicker(this, TimePicker.HOUR_24);
                end.setGravity(Gravity.CENTER);//弹框居中
                end.setRangeStart(0, 0);//09:00
                end.setRangeEnd(23, 0);//18:30
                end.setTopLineVisible(false);
                end.setOnTimePickListener(new TimePicker.OnTimePickListener() {
                    @Override
                    public void onTimePicked(String hour, String minute) {
                        end_hour = Integer.valueOf(hour);
                        end_minute = Integer.valueOf(minute);
                        end_time.setText(hour + ":" + minute);
                        showToast(hour + ":" + minute);
                    }
                });
                end.show();
                break;
            case R.id.text1:
                break;
            case R.id.text2:
                break;
            case R.id.text3:
                break;
            case R.id.text4:
                break;
            case R.id.text5:
                break;
            case R.id.text6:
                break;
            case R.id.text7:
                break;
            case R.id.sence_name_tv:
                new ChangeNameDialogUtil(SetSceneAct.this, "修改场景名称", new ChangeNameDialogUtil.SureInterfance() {
                    @Override
                    public void sureTodo(String name) {
                        //确定修改 处理方案
                        App.getApp().showToast(name);
                        sence_name_tv.setText(name);
                    }
                }).showSelfDialog();
                break;
            case R.id.scene_switch_img:
                break;
            case R.id.clean_timer_btn:
                new DialogUtil(SetSceneAct.this, true, true, "确认清除定时吗", "取消", "确定", new DialogUtil.SureInterfance() {
                    @Override
                    public void sureTodo() {
                        showToast("确定清除定时");
                        saveScene("");
                    }
                    @Override
                    public void cancle() {

                    }
                }).showCustomDialog();
                break;
        }
    }

    public void saveScene(String key) {
        progressDialog.show();
        String uid = spf.getString("Uid", "");
        String Mac = spf.getString("gatemac", "");
        String imei = spf.getString("Imei", "");

        String beginTime = "";
        String endTime = "";
        String sceneName = sence_name_tv.getText().toString().trim();
        ;
        String choosedWeeks = "";
        if (key.equals("save")) {
            beginTime = start_time.getText().toString().trim();
            endTime = end_time.getText().toString().trim();
            String weeks = "";
            if (text1.isChecked()) {
                weeks = weeks + "1,";
            }
            if (text2.isChecked()) {
                weeks = weeks + "2,";
            }
            if (text3.isChecked()) {
                weeks = weeks + "3,";
            }
            if (text4.isChecked()) {
                weeks = weeks + "4,";
            }
            if (text5.isChecked()) {
                weeks = weeks + "5,";
            }
            if (text6.isChecked()) {
                weeks = weeks + "6,";
            }
            if (text7.isChecked()) {
                weeks = weeks + "7";
            }

            if (weeks.endsWith(",")) {
                choosedWeeks = weeks.substring(0, weeks.length() - 1);
            } else {
                choosedWeeks = weeks;
            }
        }
        RetrofitService service = RetrofitHelper.getInstance(this).getServer();
        Gson gson = new Gson();

        /**
         * {
         "begin_time": "string",
         "end_time": "string",
         "frontDto": {
         "imei": "string",
         "mac": "string",
         "uid": "string"
         },
         "scene_id": "string",
         "scene_model": 0,
         "scene_name": "string",
         "scene_switch": 0,
         "weeks": "string"
         }
         */

        AddSceneBean bean = new AddSceneBean();

        bean.setBegin_time(beginTime);
        bean.setEnd_time(endTime);

        AddSceneBean.FrontDtoBean value = new AddSceneBean.FrontDtoBean();
        value.setImei(imei);
        value.setMac(Mac);
        value.setUid(uid);

        bean.setFrontDto(value);
        bean.setScene_id(scene_id);
        bean.setScene_model(2);
        bean.setScene_name(sceneName);
        bean.setScene_switch(scene_switch_img.isChecked() ? 1 : 0);
        bean.setWeeks(choosedWeeks);

        String strEntity = gson.toJson(bean);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        Call<SaveSceneBean> call = service.saveScene(body);
        call.enqueue(new Callback<SaveSceneBean>() {
            @Override
            public void onResponse(Call<SaveSceneBean> call, Response<SaveSceneBean> response) {
                progressDialog.dismiss();
                showToast("保存成功");
                finish();
            }

            @Override
            public void onFailure(Call<SaveSceneBean> call, Throwable t) {
                progressDialog.dismiss();
                showToast("保存失败");
            }
        });
    }
}
